﻿//select option是否选中
var isCheck = function (chkItem) {
    var checkedVal = $(chkItem).attr("checked");
    return !(checkedVal == undefined || eval(checkedVal) == false)
}

//字符串是否空
var isNullOrEmpty = function (str) {
    return str == null || str == "" || str == undefined;
}

var getCookie = function (name)//读取cookies函数        
{
    var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
    if (arr != null) return unescape(arr[2]); return null;

}
var setCookie = function (name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    // 使设置的有效时间正确。增加toGMTString()
    document.cookie = name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}
//计算出一个数的全排列可能
var countPermutation = function (num) {
    var rptNumDic = new Array();
    var number = num.toString();
    for (i in number) {
        var ch = number[i];
        if (isNullOrEmpty(rptNumDic[ch]))
            rptNumDic[ch] = 1;
        else
            rptNumDic[ch] = rptNumDic[ch] + 1;
    }
    var totalFac = factorial(number.length);
    $(rptNumDic).each(function (i, obj) {
        if (isNullOrEmpty(obj) || obj <= 1) return true;
        totalFac /= factorial(obj);
    });
    return totalFac;
}
//计算阶乘.
var factorial = function (num) {
    if (num == 1) return num;
    return num * factorial(--num);
}
//初始化公司号码长度信息
var InitCompanyNumLen = function () {
    CompanyNumLen = eval($.base64.decode(getCookie("CompanyNumLen")));
}
//获取QueryString
var getQuery = function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)
        return unescape(r[2]);
    return null;
}
var reloadPage = function () {
    var url = $("form").attr("action");
    window.location.href = url;
}
//调用JQuery Dialog
var showMessage = function (title, content) {
    var htmlArr = new Array();
    htmlArr.push("<div title=\"" + title + "\">");
    htmlArr.push("<p>" + content + "</p>");
    htmlArr.push("</div>");
    var msgBox = $(htmlArr.join(""));
    msgBox.appendTo($(document));
    msgBox.dialog({ modal: true, buttons: { "OK": function () {
        $(this).dialog("close");
    }
    }
    });
}
var DateAdd = function (interval, number, date) {
    /* 
    *---------------   DateAdd(interval,number,date)   ----------------- 
    *   DateAdd(interval,number,date)   
    *   功能:实现VBScript的DateAdd功能. 
    *   参数:interval,字符串表达式，表示要添加的时间间隔. 
    *   参数:number,数值表达式，表示要添加的时间间隔的个数. 
    *   参数:date,时间对象. 
    *   返回:新的时间对象. 
    *   var   now   =   new   Date(); 
    *   var   newDate   =   DateAdd( "d ",5,now); 
    *   author:wanghr100(灰豆宝宝.net) 
    *   update:2004-5-28   11:46 
    *---------------   DateAdd(interval,number,date)   ----------------- 
    */
    switch (interval) {
        case "y ":
            {
                date.setFullYear(date.getFullYear() + number);
                return date;
                break;
            }
        case "q ":
            {
                date.setMonth(date.getMonth() + number * 3);
                return date;
                break;
            }
        case "m ":
            {
                date.setMonth(date.getMonth() + number);
                return date;
                break;
            }
        case "w ":
            {
                date.setDate(date.getDate() + number * 7);
                return date;
                break;
            }
        case "d ":
            {
                date.setDate(date.getDate() + number);
                return date;
                break;
            }
        case "h ":
            {
                date.setHours(date.getHours() + number);
                return date;
                break;
            }
        case "m ":
            {
                date.setMinutes(date.getMinutes() + number);
                return date;
                break;
            }
        case "s ":
            {
                date.setSeconds(date.getSeconds() + number);
                return date;
                break;
            }
        default:
            {
                date.setDate(date.getDate() + number);
                return date;
                break;
            }
    }
}

//更新显示语言
var setLanguage = function (language) {
    setCookie("Language", language, DateAdd("m", 1, new Date()));
}
//获取当前显示语言
var getCurrentLanguage = function () {
    return getCookie("Language");
}
//集合对象中是否包含指定项
var containsItem = function (source, item) {
    var isContains = false;
    $(source).each(function (i, obj) {
        if (obj == item) {
            isContains = true;
            return false;
        }
    });
    return isContains;
}

/* 只能输入数字
例如：$(input[type='text']).numeral();
*/
$.fn.numeral = function (isSupportZero) {
    $(this).css("ime-mode", "disabled");
    this.bind("keypress", function () {
        if (event.keyCode == 46) {
            if (this.value.indexOf(".") != -1) {
                return false;
            }
        } else {
            return event.keyCode >= 46 && event.keyCode <= 57;
        }
    });
    this.bind("blur", function () {
        if (this.value.lastIndexOf(".") == (this.value.length - 1)) {
            this.value = this.value.substr(0, this.value.length - 1);
        } else if (isNaN(this.value)) {
            this.value = "";
        }
    });
    this.bind("paste", function () {
        var s = clipboardData.getData('text');
        if (!/\D/.test(s));
        if (!isSupportZero)
            value = s.replace(/^0*/, '');
        return false;
    });
    this.bind("dragenter", function () {
        return false;
    });
    this.bind("keyup", function () {
        if (/(^0+)/.test(this.value)) {
            if (!isSupportZero)
                this.value = this.value.replace(/^0*/, '');
        }
    });
};

//增加回发参数
var addAction = function (action/*参数名*/, value/*参数值*/, clear/*是否清空参数*/) {
    //获取当前页Url
    var formUrl = $("form")[0].action;
    //如果Url中含有参数
    if (formUrl.indexOf("?") > 0) {
        //清空参数
        if (clear)
            formUrl = formUrl.substr(0, formUrl.indexOf("?") + 1);
        //Url中含有要添加的参数对
        if (formUrl.indexOf(action) > 0) {
            //获取所有参数对值
            var queryString = formUrl.substr(formUrl.indexOf("?") + 1, formUrl.length);
            //分组
            var queryStringArr = queryString.split("&");
            //不包含参数的Url
            formUrl = formUrl.substr(0, formUrl.indexOf("?") + 1);
            var isFirst = true;
            //遍历参数对
            for (var i = 0; i < queryStringArr.length; i++) {
                //如果参数对不等于要添加的Action.则添加到Url中
                if (queryStringArr[i].indexOf(action) < 0) {
                    if (isFirst) isFirst = false;
                    else formUrl += "&";
                    formUrl += queryStringArr[i];
                }
            }
        }
        //避免只有一个参数,并且参数相同时出现的?&情况
        if (formUrl.indexOf("?") + 1 != formUrl.length)
            formUrl += "&";
    }
    else {
        formUrl += "?";
    }

    formUrl += action + "=" + value;
    $("form")[0].action = formUrl;
}

//本页回发
var postBack = function (action/*参数名*/, value/*参数值*/, clear/*是否清空参数*/) {
    if (action != null && value != null && value != undefined && value != "undefined")
        addAction(action, value, clear);
    var formUrl = $("form")[0].action;
    window.location.href = formUrl;
}

var buildFrame = function (frameId, height, width, src) {
    var div = $("<div></div>");
    var iframe = $("<iframe id=\"" + frameId + "\" Width=\"" + width + "px\" height=\"" + height + "px\" frameborder=\"0\" src=\"" + src + "\" </iframe>").appendTo(div);
    div.appendTo($(document));
    var height = $("#frame").attr("height");
    div.dialog({ modal: true, minWidth: width, minHeight: height });
}

var bindAmountSlider = function (notClass) {
    $("input[type='text']:not(." + notClass + ")").bind("focus", function () {
        var offset = $(this).offset();
        var betBoard = $("#betBoard");
        var thisId = $(this).attr("id");
        if (betBoard.length == 0)
            betBoard = buildBetBoard(thisId);
        else {
            betBoard.attr("parent", thisId);
            $("#slider").slider({ value: 0 });
        }
        betBoard.css("top", offset.top + 20).css("left", offset.left).css("position", "absolute").css("z-index", "999");
        betBoard.show();
    });
}
var buildBetBoard = function (input) {
    var betBoard = $("<div id='betBoard'></div>");
    betBoard.css("height", "20px").css("width", "150px");
    betBoard.attr("parent", input);
    var silder = $("<div id=\"slider\"></div>").appendTo(betBoard);
    $(silder).slider({
        range: "min",
        value: 37,
        min: 0,
        max: 5000,
        step: 100,
        slide: function (event, ui) {
            var parentId = $(this).parent().attr("parent");
            var parent = $("#" + parentId);
            parent.val(ui.value);
        }
    });
    $(silder).find("a").bind("blur", function () { $("#betBoard").hide(); });
    betBoard.appendTo($("body"));
    return betBoard;
}